﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Xml;

namespace SerialisationProject
{
    /// <summary>
    /// This class is used to serialise/deserialise objects and xml
    /// </summary>
    [XmlRootAttribute(ElementName = "Batch", IsNullable = false)]
    public class Batch
    {
        [XmlElement(ElementName="Tasks")]
        public List<Task> Tasks = new List<Task>();

        #region Serialize
        /// <summary>
        /// Method to convert a custom Object to XML string
        /// </summary>
        /// <param name="pObject">Object that is to be serialized to XML</param>
        /// <returns>XML string</returns>
        public string Serialize()
        {
            try
            {
                String XmlizedString = null;
                MemoryStream memoryStream = new MemoryStream();
                XmlSerializer xs = new XmlSerializer(typeof(Batch));
                XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);

                xs.Serialize(xmlTextWriter, this);
                memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
                XmlizedString = ConversionHelper.UTF8ByteArrayToString(memoryStream.ToArray());
                return XmlizedString;
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e);
                return null;
            }
        }
        #endregion

        #region Deserialize
        /// <summary>
        /// Method to reconstruct an Object from XML string
        /// </summary>
        /// <param name="pXmlizedString"></param>
        /// <returns></returns>
        public static object Deserialize(string pXmlizedString)
        {
            XmlSerializer xs = new XmlSerializer(typeof(Batch));
            MemoryStream memoryStream = new MemoryStream(ConversionHelper.StringToUTF8ByteArray(pXmlizedString));
            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);

            return xs.Deserialize(memoryStream);
        }
        #endregion
    }

    public class Task
    {
        private string m_spWebUrl;
        private string m_listName;
        private string m_fieldName;
        private string m_lookupListName;
        private string m_lookupListFieldName;

        public string SPWebUrl
        {
            get { return m_spWebUrl; }
            set { m_spWebUrl = value; }
        }

        public string ListName
        {
            get { return m_listName; }
            set { m_listName = value; }
        }

        public string FieldName
        {
            get { return m_fieldName; }
            set { m_fieldName = value; }
        }

        public string LookupListName
        {
            get { return m_lookupListName; }
            set { m_lookupListName = value; }
        }

        public string LookupListFieldName
        {
            get { return m_lookupListFieldName; }
            set { m_lookupListFieldName = value; }
        }
    }
}
